<?php

namespace App\Http\Controllers\ChatGroup\Merchant;

use App\Http\Controllers\BaseController;
use App\Models\ChatGroup\ChatGroupHistoryModel;
use App\Models\ChatGroup\ChatGroupUserModel;
use App\Service\ChatGroup\ChatGroupMessageReadService;
use App\Service\ChatGroup\ChatGroupSessionService;
use App\Service\ChatGroup\ChatGroupStructService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\RequiredIf;

class ChatMessageController extends BaseController
{
    public function send(Request $request)
    {
        $shopId      = $request->header('shopId');
        $groupId     = $request->get('groupId');
        $type        = $request->get('type');
        $content     = $request->get('content');
        $clientMsgId = (string)$request->get('clientMsgId');
        $faqId       = (int)$request->get('faqId');

        $userId = getRmsUserId();

        $this->validate($request, [
            'groupId'     => 'required|integer|min:1',
            'clientMsgId' => 'required|string|min:1',
            'type'        => ['required', 'string', Rule::in(array_keys(ChatGroupHistoryModel::TYPES))],
            'content'     => 'required',
            'faqId'       => 'integer',
        ]);

        (new ChatGroupStructService($groupId, $shopId))
            ->sendMsgStruct($userId, $clientMsgId, $type, $content, $faqId, 0, ChatGroupHistoryModel::IS_SEND_AI_YES)
            ->send();

        return $this->success();
    }

    public function clearUnread(Request $request)
    {
        $groupId     = $request->get('groupId');
        $userId = getRmsUserId();

        (new ChatGroupMessageReadService($userId))->readMessage($groupId);

        return $this->success();
    }
}
